﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using Ignitron.EEngine;

namespace Board3D
{
    /// <summary>
    /// The computed background image
    /// </summary>
    public class Background : IBackground
    {
        /// <summary>
        /// Computed texture private data
        /// </summary>
        private int mTextureSize;
        private byte[, ,] mData;

        /// <summary>
        /// Returns the width
        /// </summary>
        public int Width
        {
            get { return mTextureSize; }
        }

        /// <summary>
        /// Returns the height
        /// </summary>
        public int Height
        {
            get { return mTextureSize; }
        }

        /// <summary>
        /// Returns the pixel data 
        /// </summary>
        public byte[, ,] Pixels
        {
            get { return mData; }
        }

        /// <summary>
        /// Computes the image
        /// </summary>
        public void Compute(int size, Color startColor, Color endColor)
        {
            mTextureSize = size;

            mData = new byte[size, size, 3];
            double sizeHalf = size / 2;
            double scaleFactor = 1.1;

            for (int x = 0; x < size; x++)
                for (int y = 0; y < size; y++)
                {
                    double xx = (x - sizeHalf) / sizeHalf;
                    double yy = (y - sizeHalf) / sizeHalf;
                    double factor = scaleFactor * Convert.ToDouble(Math.Sqrt(Math.Pow(xx, 2) + Math.Pow(yy, 2)));
                    if (factor < 0)
                        factor = 0;
                    if (factor > 1)
                        factor = 1;

                    int red = (int)Math.Round((1 - factor) * (double)startColor.R + factor * (double)endColor.R);
                    int green = (int)Math.Round((1 - factor) * (double)startColor.G + factor * (double)endColor.G);
                    int blue = (int)Math.Round((1 - factor) * (double)startColor.B + factor * (double)endColor.B);

                    mData[x, y, 0] = (byte)red;
                    mData[x, y, 1] = (byte)green;
                    mData[x, y, 2] = (byte)blue;
                }
        }
    }
}
